package com.bweab.controll.userstbcontrol;

import com.bweab.pojo.UsersTb;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/checkUsername")
public class CheckUserName extends HttpServlet {

    JSONResult jsonResult=new JSONResult();
    UsersTbServiceInterface userService = new UsersTbServiceInterfaceImpl();//调用user服务端接口

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户名
        String username = req.getParameter("username");

        //调用服务层接口
        UsersTb userInfo = userService.getUserInfo(username);
        System.out.println(userInfo);
        if(userInfo==null){
            //可用
            jsonResult.setStatusCode(JSONStats.REGIST_OK.getStatusCode());
            jsonResult.setMessage(JSONStats.REGIST_OK.getMessage());
            WebUtil.writeJson(resp,jsonResult);
        }else {
            //不可用
            jsonResult.setStatusCode(JSONStats.REGIST_SPAN.getStatusCode());
            jsonResult.setMessage(JSONStats.REGIST_SPAN.getMessage());
            WebUtil.writeJson(resp,jsonResult);
        }
    }
}
